package com.example.ccashbook.ui

import android.content.Intent
import android.media.MediaPlayer
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.example.ccashbook.R
import com.example.ccashbook.base.BaseMainActivity
import com.example.ccashbook.fragment.MxFragment
import com.example.ccashbook.fragment.TbFragment
import com.example.ccashbook.fragment.WdFragment

class MainActivity : BaseMainActivity() {
    private lateinit var mFragments: Array<Fragment?>
    private var mMxFragment: MxFragment? = null
    private var mTbFragment: TbFragment? = null
    private var mWdFragment: WdFragment? = null
    private var index = 0
    private var currentTabIndex = 0
    private lateinit var mTabs: Array<LinearLayout?>
    private var mediaPlayer: MediaPlayer? = null
    protected override fun setLayoutResourceID(): Int {
        return R.layout.activity_main
    }

    protected override fun initView() {
        //初始化添加fragment
        mMxFragment = MxFragment()
        mTbFragment = TbFragment()
        mWdFragment = WdFragment()
        mFragments = arrayOf(mMxFragment, mTbFragment, mWdFragment)

        //默认显示第一个fragment
        getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, mMxFragment)
            .show(mMxFragment)
            .commit()

        //每一个底部做监听
        mTabs = arrayOfNulls(mFragments.size)
        mTabs[0] = findViewById(R.id.mx)
        mTabs[1] = findViewById(R.id.tb)
        mTabs[2] = findViewById(R.id.wd)
        mTabs[0]!!.setOnClickListener(this)
        mTabs[1]!!.setOnClickListener(this)
        mTabs[2]!!.setOnClickListener(this)


        //设置第一个为选中状态
        mTabs[0]!!.isSelected = true
        mediaPlayer = MediaPlayer.create(this, R.raw.a)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        getMenuInflater().inflate(R.menu.menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.about -> AlertDialog.Builder(this@MainActivity)
                .setTitle("关于作者")
                .setMessage("这是一个随时随地都可以使用的记账本")
                .setNegativeButton("确定", null)
                .show()
            R.id.bg -> sendBroadcast(Intent("bg"))
            R.id.music -> if (mediaPlayer!!.isPlaying) {
                mediaPlayer!!.pause()
            } else {
                mediaPlayer!!.start()
            }
        }
        return true
    }

    fun onClick(v: View) {
        //点击改变下标
        when (v.id) {
            R.id.mx -> index = 0
            R.id.tb -> index = 1
            R.id.wd -> index = 2
        }
        setIndex()
    }

    fun setIndex() {
        //如果点击的是另外一个下标
        if (currentTabIndex != index) {
            val trx: FragmentTransaction = getSupportFragmentManager().beginTransaction()
            trx.hide(mFragments[currentTabIndex]!!) //隐藏当前的页面
            if (!mFragments[index]!!.isAdded) { //如果新的页面没有显示过，则添加
                trx.add(R.id.fragment_container, mFragments[index]!!)
            }
            //显示当前选中的页面
            trx.show(mFragments[index]!!).commitAllowingStateLoss()
        }
        mTabs[currentTabIndex]!!.isSelected = false
        mTabs[index]!!.isSelected = true
        //把当前显示的下标赋值给currentTabIndex
        currentTabIndex = index
    }
}